package com.cloud.edu.sys.service.impl;

import com.cloud.edu.sys.dao.DictionaryMapper;
import com.cloud.edu.sys.dao.SqlAdapterMapper;
import com.cloud.edu.sys.model.Dictionary;
import com.cloud.edu.sys.model.DictionaryExample;
import com.cloud.edu.sys.model.UserExample;
import com.cloud.edu.sys.service.SysDicService;
import com.cloud.edu.utils.PageUtils;
import com.cloud.edu.utils.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("sysDicService")
public class SysDicServiceImpl  implements SysDicService {
	@Autowired
	private DictionaryMapper dictionaryMapper;
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao

	/**
	 * 分页数据
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils=PageUtils.getPageInfo(params);
		Page<Object> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		StringBuffer sql=new StringBuffer(" select * from sys_dictionary  ORDER BY CLASS_CODE,DATA_CODE ");
		sqlAdapterMapper.selectSQL(sql.toString());
		pageUtils =new PageUtils(p);
		return pageUtils;
	}
	
	@Override
	public void save(Dictionary dictionary) {
		dictionary.setId(UuidUtil.get32UUID());
		dictionaryMapper.insert(dictionary);
	}

	@Override
	public void update(Dictionary dictionary) {
	}

	@Override
	public void updateValueByKey(String key, String value) {
	}

	@Override
	public void deleteBatch(String[] ids) {
		for(String id:ids){
			dictionaryMapper.deleteByPrimaryKey(id);
		}
		
	}

	@Override
	public String getValue(String key) {
		return key;
		
	}
	
	/**
	 * 主键获取字典
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Dictionary selectById(String id) throws Exception{
		Dictionary dictionary=dictionaryMapper.selectByPrimaryKey(id);
		return dictionary;
	}
	
	@Override
	public <T> T getConfigObject(String key, Class<T> clazz) {
		return null;
	}
	
	@Override
	public List<Dictionary> getList(Dictionary dictionary) throws Exception{
		DictionaryExample example = new DictionaryExample();
		com.cloud.edu.sys.model.DictionaryExample.Criteria criteria= example.createCriteria();
		if(dictionary.getClassCode()!=null&&!("").equals(dictionary.getClassCode())){
			criteria.andClassCodeEqualTo(dictionary.getClassCode());
		}
		List<Dictionary> list=dictionaryMapper.selectByExample(example);
		return list;
	}
}
